2
תגובות
אהלן. אני די חדש ב-laravel ומנסה כבר כמה שעות ללא הצלחה לאפשר כניסת משתמש באמצעות מערכת ה-authentication. משום מה, הכניסה נכשלת פעם אחר פעם.
הנה הקודים:
User model:
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
protected $primaryKey = 'id';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $fillable = array('username', 'email', 'password');
protected $hidden = array('password');
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
}
Route:
Route::get('admin', array(
'as' => 'admin',
'uses' => 'AdminController@login'
)
);
Route::post('login', array(
'as' => 'login',
'uses' => 'AdminController@Checklogin'
)
);
AdminController:
<?php
class AdminController extends BaseController {
public function login()
{
return View::make('admin.index');
}
public function Checklogin() {
$rules = array(
'username' => 'required', // make sure the email is an actual email
'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
);
// run the validation rules on the inputs from
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('admin')
->withErrors($validator) // send back all errors to the login form
->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
} else {
// create our user data for the authentication
//$user = User::where('username', '=' , $username)->where('password', '=', $password)- >get();
// $user->count()
$auth = Auth::attempt(array(
'username' => Input::get('username'),
'password' => Input::get('password')
));
var_dump(Input::get('password'));
if($auth) {
return "Successful login";
}
}
}
auth.php
<?php
return array(
'driver' => 'eloquent',
'model' => 'User',
'table' => 'users',
'username' => 'username',
'password' => 'password',
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
);
the view:
{{ Form::open(array('url' => 'login', 'class' => 'form-horizontal')) }}
<!-- Name -->
<div class="control-group {{{ $errors->has('username') ? 'error' : '' }}}">
{{ Form::label('username', 'שם משתמש', array('class' => 'control-label')) }}
<div class="controls">
{{ Form::text('username', Input::old('username')) }}
{{ $errors->first('username') }}
</div>
</div>
<!-- Password -->
<div class="control-group {{{ $errors->has('password') ? 'error' : '' }}}">
{{ Form::label('password', 'סיסמה', array('class' => 'control-label')) }}
<div class="controls">
{{ Form::password('password') }}
{{ $errors->first('password') }}
</div>
</div>
<!-- Login button -->
<div class="control-group">
<div class="controls">
{{ Form::submit('התחבר', array('class' => 'btn')) }}
</div>
</div>
{{ Form::close() }}
הנה הקודים:
User model:
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
protected $primaryKey = 'id';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $fillable = array('username', 'email', 'password');
protected $hidden = array('password');
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
}
Route:
Route::get('admin', array(
'as' => 'admin',
'uses' => 'AdminController@login'
)
);
Route::post('login', array(
'as' => 'login',
'uses' => 'AdminController@Checklogin'
)
);
AdminController:
<?php
class AdminController extends BaseController {
public function login()
{
return View::make('admin.index');
}
public function Checklogin() {
$rules = array(
'username' => 'required', // make sure the email is an actual email
'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
);
// run the validation rules on the inputs from
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('admin')
->withErrors($validator) // send back all errors to the login form
->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
} else {
// create our user data for the authentication
//$user = User::where('username', '=' , $username)->where('password', '=', $password)- >get();
// $user->count()
$auth = Auth::attempt(array(
'username' => Input::get('username'),
'password' => Input::get('password')
));
var_dump(Input::get('password'));
if($auth) {
return "Successful login";
}
}
}
auth.php
<?php
return array(
'driver' => 'eloquent',
'model' => 'User',
'table' => 'users',
'username' => 'username',
'password' => 'password',
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
);
the view:
{{ Form::open(array('url' => 'login', 'class' => 'form-horizontal')) }}
<!-- Name -->
<div class="control-group {{{ $errors->has('username') ? 'error' : '' }}}">
{{ Form::label('username', 'שם משתמש', array('class' => 'control-label')) }}
<div class="controls">
{{ Form::text('username', Input::old('username')) }}
{{ $errors->first('username') }}
</div>
</div>
<!-- Password -->
<div class="control-group {{{ $errors->has('password') ? 'error' : '' }}}">
{{ Form::label('password', 'סיסמה', array('class' => 'control-label')) }}
<div class="controls">
{{ Form::password('password') }}
{{ $errors->first('password') }}
</div>
</div>
<!-- Login button -->
<div class="control-group">
<div class="controls">
{{ Form::submit('התחבר', array('class' => 'btn')) }}
</div>
</div>
{{ Form::close() }}
2 תשובות
תוודא שהסיסמא בטבלה users מוצפנת,במידה וכן תציג כאן חלק מהנתונים שיש בטבלה ובנוסף את השגיאה שקיבלת בטופס
תגיות [ code ] ?
בכל מקרה אני לא רואה בעיה בקוד, כמו ש@Splash אמר ואני ארחיב, המחלקה Auth משתמשת במחלקה Hash להשוואת הסיסמאות, אז שאתה יוצר משתמש חדש במסד הסיסמא שלו צריכה להיות מוצפנת.
או בקוד:
$credentials = [
'username' => 'ldbrgr',
'password' => 'secret'
];
$user = User::create([
'username' => array_get($credentials, 'username'),
'password' => Hash::make(array_get($credentials, 'secret')) // .. Hash::make($password)
]);
Auth::attempt($credentials);
'username' => 'ldbrgr',
'password' => 'secret'
];
$user = User::create([
'username' => array_get($credentials, 'username'),
'password' => Hash::make(array_get($credentials, 'secret')) // .. Hash::make($password)
]);
Auth::attempt($credentials);
הבנת?
טיפ: תשתמש בMutator method כדי להצפין את הסיסמא ביצירת\עדכון המודל.